#ifndef SERVER_HPP_
#define SERVER_HPP_

#include <string>
#include "system/socket/Socket.hpp"
#include "system/socket/AbstractConnectionProcessor.hpp"
#include "system/thread/Thread.hpp"

namespace ServerServer
{
	/**
	 * This class keeps the logic of server that is runing in a seperate thread.
	 * The server uses non blocking accept so we can check if the thread should be stopped
	 * or not.
	 */
	class Server : public SystemThread::Thread
	{
	public:
		/**
		 * The method creates the server on port iPort, with the maximum connections on socket
		 * specified in the iMaxQueueSize variable. It also takes as a parameter implementation
		 * of AbstractConnectionProcessor that will process incoming connections.
		 *
		 * \param iPortNumber the port that the server should run on.
		 * \param iMaxQueueSize the maximum number of connections on the socket.
		 * \param iProcessor the implementation of connaction handler.
		 */
		Server(
				unsigned int iPortNumber,
				int iMaxQueueSize,
				SystemSocket::AbstractConnectionProcessor * iProcessor);
		/**
		 * The destructor just tries to close the socket.
		 */
		virtual ~Server();
	protected:
		/**
		 * Create the socket and try to accept incoming connections. If no connections, then
		 * check if the thread was asked to be killed and exit. The method passes every incoming
		 * connection to connection processor.
		 */
		virtual void runThreadImpl();
		// the socket that is used for the server
		SystemSocket::Socket _socket;
		// process incoming connections
		SystemSocket::AbstractConnectionProcessor * _processor;

	};
}

#endif /* SERVER_HPP_ */
